package singleton;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * Created by User on 2017/12/13.
 */
public class Singleton {
    private static Singleton singleton = null;

    private Singleton(){}

    static ReadWriteLock lock = new ReentrantReadWriteLock();

    public static Singleton getSingleton(){
        lock.readLock().lock();
        try {
            if (singleton == null) {
                synchronized (Singleton.class) {
                    if (singleton == null) {
                        lock.writeLock().lock();
                        try {
                            singleton = new Singleton();
                        } finally {
                            lock.writeLock().unlock();
                        }
                    }
                }
            }

            return singleton;
        } finally {
            lock.readLock().unlock();
        }
    }

    public static void destroy() {
        lock.writeLock().lock();
        try {
            singleton = null;
        } finally {
            lock.writeLock().unlock();
        }
    }

    @Override
    public boolean equals(Object obj) {
        return super.equals(obj);
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }
}